package com.google.gson.b;

import com.sutuijingling.result.WorkStatus;
import java.io.Closeable;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class d implements Closeable {
    private final Writer a;
    private final List b = new ArrayList();
    private String c;
    private String d;
    private boolean e;
    private boolean f;
    private String g;
    private boolean h;

    public d(Writer writer) {
        this.b.add(b.EMPTY_DOCUMENT);
        this.d = ":";
        this.h = true;
        if (writer == null) {
            throw new NullPointerException("out == null");
        }
        this.a = writer;
    }

    private b a() {
        return (b) this.b.get(this.b.size() - 1);
    }

    private d a(b bVar, b bVar2, String str) throws IOException {
        b a = a();
        if (a != bVar2 && a != bVar) {
            throw new IllegalStateException("Nesting problem: " + this.b);
        } else if (this.g != null) {
            throw new IllegalStateException("Dangling name: " + this.g);
        } else {
            this.b.remove(this.b.size() - 1);
            if (a == bVar2) {
                k();
            }
            this.a.write(str);
            return this;
        }
    }

    private d a(b bVar, String str) throws IOException {
        e(true);
        this.b.add(bVar);
        this.a.write(str);
        return this;
    }

    private void a(b bVar) {
        this.b.set(this.b.size() - 1, bVar);
    }

    private void d(String str) throws IOException {
        this.a.write("\"");
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            switch (charAt) {
                case '\b':
                    this.a.write("\\b");
                    break;
                case '\t':
                    this.a.write("\\t");
                    break;
                case '\n':
                    this.a.write("\\n");
                    break;
                case '\f':
                    this.a.write("\\f");
                    break;
                case WorkStatus.ItemMakeOrder /*13*/:
                    this.a.write("\\r");
                    break;
                case '\"':
                case '\\':
                    this.a.write(92);
                    this.a.write(charAt);
                    break;
                case '&':
                case '\'':
                case '<':
                case '=':
                case '>':
                    if (!this.f) {
                        this.a.write(charAt);
                        break;
                    }
                    this.a.write(String.format("\\u%04x", new Object[]{Integer.valueOf(charAt)}));
                    break;
                case ' ':
                case ' ':
                    this.a.write(String.format("\\u%04x", new Object[]{Integer.valueOf(charAt)}));
                    break;
                default:
                    if (charAt > '\u001f') {
                        this.a.write(charAt);
                        break;
                    }
                    this.a.write(String.format("\\u%04x", new Object[]{Integer.valueOf(charAt)}));
                    break;
            }
        }
        this.a.write("\"");
    }

    private void e(boolean z) throws IOException {
        switch (a()) {
            case EMPTY_DOCUMENT:
                if (this.e || z) {
                    a(b.NONEMPTY_DOCUMENT);
                    return;
                }
                throw new IllegalStateException("JSON must start with an array or an object.");
            case EMPTY_ARRAY:
                a(b.NONEMPTY_ARRAY);
                k();
                return;
            case NONEMPTY_ARRAY:
                this.a.append(',');
                k();
                return;
            case DANGLING_NAME:
                this.a.append(this.d);
                a(b.NONEMPTY_OBJECT);
                return;
            case NONEMPTY_DOCUMENT:
                throw new IllegalStateException("JSON must have only one top-level value.");
            default:
                throw new IllegalStateException("Nesting problem: " + this.b);
        }
    }

    private void j() throws IOException {
        if (this.g != null) {
            l();
            d(this.g);
            this.g = null;
        }
    }

    private void k() throws IOException {
        if (this.c != null) {
            this.a.write("\n");
            for (int i = 1; i < this.b.size(); i++) {
                this.a.write(this.c);
            }
        }
    }

    private void l() throws IOException {
        b a = a();
        if (a == b.NONEMPTY_OBJECT) {
            this.a.write(44);
        } else if (a != b.EMPTY_OBJECT) {
            throw new IllegalStateException("Nesting problem: " + this.b);
        }
        k();
        a(b.DANGLING_NAME);
    }

    public d a(long j) throws IOException {
        j();
        e(false);
        this.a.write(Long.toString(j));
        return this;
    }

    public d a(Number number) throws IOException {
        if (number == null) {
            return f();
        }
        j();
        CharSequence obj = number.toString();
        if (this.e || !(obj.equals("-Infinity") || obj.equals("Infinity") || obj.equals("NaN"))) {
            e(false);
            this.a.append(obj);
            return this;
        }
        throw new IllegalArgumentException("Numeric values must be finite, but was " + number);
    }

    public d a(String str) throws IOException {
        if (str == null) {
            throw new NullPointerException("name == null");
        } else if (this.g != null) {
            throw new IllegalStateException();
        } else {
            this.g = str;
            return this;
        }
    }

    public d a(boolean z) throws IOException {
        j();
        e(false);
        this.a.write(z ? "true" : "false");
        return this;
    }

    public d b() throws IOException {
        j();
        return a(b.EMPTY_ARRAY, "[");
    }

    public d b(String str) throws IOException {
        if (str == null) {
            return f();
        }
        j();
        e(false);
        d(str);
        return this;
    }

    public final void b(boolean z) {
        this.e = z;
    }

    public d c() throws IOException {
        return a(b.EMPTY_ARRAY, b.NONEMPTY_ARRAY, "]");
    }

    public final void c(String str) {
        if (str.length() == 0) {
            this.c = null;
            this.d = ":";
            return;
        }
        this.c = str;
        this.d = ": ";
    }

    public final void c(boolean z) {
        this.f = z;
    }

    public void close() throws IOException {
        this.a.close();
        if (a() != b.NONEMPTY_DOCUMENT) {
            throw new IOException("Incomplete document");
        }
    }

    public d d() throws IOException {
        j();
        return a(b.EMPTY_OBJECT, "{");
    }

    public final void d(boolean z) {
        this.h = z;
    }

    public d e() throws IOException {
        return a(b.EMPTY_OBJECT, b.NONEMPTY_OBJECT, "}");
    }

    public d f() throws IOException {
        if (this.g != null) {
            if (this.h) {
                j();
            } else {
                this.g = null;
                return this;
            }
        }
        e(false);
        this.a.write("null");
        return this;
    }

    public boolean g() {
        return this.e;
    }

    public final boolean h() {
        return this.f;
    }

    public final boolean i() {
        return this.h;
    }
}
